package cn.gameback.r03_24311133.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.jakarta.StatViewFilter;
import com.alibaba.druid.support.jakarta.WebStatFilter;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

// 配置类
@Configuration
@ConfigurationProperties("spring.datasource.druid")
@Data
public class DruidConfig {
    private HashMap<String, String> statViewServlet;
    // 读取yml 文件
    private HashMap<String, String> webStatFilter;

    @Bean
    public DruidDataSource druidDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    // 配置监控页面
    public FilterRegistrationBean<StatViewFilter> druidStatViewFilter() {
        StatViewFilter filter = new StatViewFilter();
        FilterRegistrationBean<StatViewFilter> bean = new FilterRegistrationBean<StatViewFilter>(filter);
        bean.addUrlPatterns(statViewServlet.get("urlPattern"));
        bean.setInitParameters(statViewServlet);
        return bean;
    }

    @Bean
    // url 监控配置
    public FilterRegistrationBean<WebStatFilter> druidWebStatFilter() {
        WebStatFilter filter = new WebStatFilter();
        FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<WebStatFilter>(filter);
        bean.addUrlPatterns(webStatFilter.get("urlPattern"));
        bean.setInitParameters(webStatFilter);
        return bean;
    }

}
